package com.jhy.review.huiyi;

import java.util.Arrays;

/**
 * @author go_jhy
 * @create 2022/6/8 - 上午 10:37
 */
@SuppressWarnings("all")
public class Code01_SelectionSort {
    public static void selectionSort(int[] arr) {
        if(arr == null || arr.length<2){
            return;
        }
        for (int i = 0; i <  arr.length-1 ; i++) {
            //先记录i的坐标
            int minIndex = i;
            for (int j = i + 1;j<arr.length;j++){
                minIndex = arr[j] < arr[minIndex] ? j  : minIndex;
            }
            swap(arr,i,minIndex);
        }

    }
    public static void swap(int[] arr,int i,int j){
        int temp =  arr[i];
        arr[i] = arr[j];
        arr[j] = temp;

    }

    public static void main(String[] args) {
        int[] arr = {5, 4, 3, 7, 9, 1};
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
    }

}
